package com.defense;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class Login extends JFrame implements ActionListener {

    // 定义组件
    JButton jb1, jb2 = null;
    JPanel jp1, jp2, jp3, jp4 = null;
    JTextField jtf = null;
    JLabel jlb1, jlb2 = null;
    JPasswordField jpf = null;

    // 设定用户名和密码
    final String stu_name = "admin";
    final String stu_pwd = "123456";

    public static void main(String[] args) {
        Login ms = new Login();
    }

    public Login() {
        // 创建组件
        jb1 = new JButton("登录");
        jb2 = new JButton("取消");
        // 设置监听
        jb1.addActionListener(this);
        jb2.addActionListener(this);

        jp1 = new JPanel();
        jp2 = new JPanel();
        jp3 = new JPanel();
        jp4 = new JPanel();

        jlb1 = new JLabel("账号：");
        jlb2 = new JLabel("密码：");

        jtf = new JTextField(10);
        jpf = new JPasswordField(10);
        // 加入到JPanel中
        jp1.add(jlb1);
        jp1.add(jtf);

        jp2.add(jlb2);
        jp2.add(jpf);

        jp4.add(jb1);
        jp4.add(jb2);

        // 加入JFrame中
        this.add(jp1);
        this.add(jp2);
        this.add(jp4);
        // 设置布局管理器
        this.setLayout(new GridLayout(3, 1));
        // 给窗口设置标题
        this.setTitle("登录系统");
        // 设置窗体大小
        this.setSize(300, 200);

        // 获取屏幕尺寸信息
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;

        int x = (screenWidth - this.getWidth()) / 2;
        int y = (screenHeight - this.getHeight()) / 2;
        this.setLocation(x, y);

        // 设置当关闭窗口时，保证JVM也退出
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 显示窗体
        this.setVisible(true);
        this.setResizable(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand() == "登录") {
            login();
        } else if (e.getActionCommand() == "取消") {
            clear();
        }

    }


    // 登录判断方法
    public void login() {
        if (stu_name.equals(jtf.getText()) && stu_pwd.equals(jpf.getText())) {
            JOptionPane.showMessageDialog(null, "登录成功！", "提示消息", JOptionPane.WARNING_MESSAGE);
            clear();
            // 关闭当前窗口
            dispose();

            // 创建MangerInterface实例并显示界面
            MangerInterface mangerInterface = new MangerInterface();
            mangerInterface.showInterface();

        } else if (jtf.getText().isEmpty() && jpf.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "请输入账号和密码！", "提示消息", JOptionPane.WARNING_MESSAGE);
        } else if (jtf.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "请输入账号！", "提示消息", JOptionPane.WARNING_MESSAGE);
        } else if (jpf.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "请输入密码！", "提示消息", JOptionPane.WARNING_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "账号或者密码错误！\n请重新输入", "提示消息", JOptionPane.ERROR_MESSAGE);
            // 清空输入框
            clear();
        }
    }

    // 清空文本框和密码框
    public void clear() {
        jtf.setText("");
        jpf.setText("");
    }
}